﻿package model.login;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.jboss.netty.channel.Channel;

import com.sun.xml.internal.bind.v2.model.core.ID;

import manager.ManagerPlayers;
import manager.Player;
import model.db.DBserver;
import model.db.vo.TableNameVo;
import model.memCached.MemCachedManage;
import model.message.MessageEncoder;
import model.message.MessageVo;
import protocol.login.Login_protocol;
import protocol.map.Map_protocol;
import protocol.role.Role_protocol;
import queue.KSocketQueueCenter;
import send.Sender;
import config.LoginConfig;
import config.MapConfig;
import config.SystemDefine;
import util.KIntChange;
import util.KPrintInfo;

public class LoginServer {
	static private LoginServer	loginServer;
	
	static public LoginServer getInstance() {
		if (loginServer == null) {
			loginServer = new LoginServer();
		}
		return loginServer;
	}
	
	public int observerUserHas(String names) {
		// SELECT*
		// FROM`tb_user`
		// WHERE`Name`="1"
		
		ArrayList fields = new ArrayList();
		fields.add("name");
		fields.add("id");
		fields.add("sex");
		fields.add("level");
		
		String selCondition = " where name = " + "'" + names + "'";
		
		ArrayList list = DBserver.getInstance().dbSelect("t_user", fields, selCondition);
		
		if (list.size() > 0) {
			HashMap memcaHashMap = (HashMap) list.get(0);
			String idString = (String) memcaHashMap.get("id");
			Integer id = KIntChange.intChange(idString);
			MemCachedManage.getInstance().add(TableNameVo.T_user + id, memcaHashMap);
			return id;
		} else {
			return 0;
		}
	}
	
	/**
	 * 
	 * @param names
	 * @param channel
	 * @throws IOException
	 */
	public void Login(Login_protocol protocol, Channel channel) throws IOException {
		
		int id = observerUserHas(protocol.getNames());
		
		if (id == 0) {
			// /不存在
			protocol.setResult(0);
			protocol.setErrorId(LoginConfig.getInstance().getNoHaveUserID());
		} else {
			protocol.setResult(1);
		}
		
		byte[] bytarr = protocol.write();
		
		try {
			channel.write(bytarr);
		} catch (Exception e) {
			KPrintInfo.println("LoginServer-------Login");
			KPrintInfo.println(e);
		}
		
		// 推送个人信息
		if (id > 0) {
			
			Player player = new Player(channel);
			ManagerPlayers.getInstance().add(id, player);
			
			sendRole(channel, id);
		}
	}
	
	private void sendRole(Channel channel, int id) throws IOException {
		//
		HashMap maps = (HashMap) MemCachedManage.getInstance().get(TableNameVo.T_user + id);
		
		ArrayList fields = new ArrayList();
		fields.add("mapid");
		fields.add("y");
		fields.add("x");
		fields.add("roleid");
		
		// /enter map
		String selCondition = " where roleid = " + "'" + id + "'";
		ArrayList list = DBserver.getInstance().dbSelect("t_lastmap", fields, selCondition);
		if (list.size() > 0) {
			HashMap memcaHashMap = (HashMap) list.get(0);
			MemCachedManage.getInstance().add(TableNameVo.T_lastMap + id, memcaHashMap);
			
			Map_protocol map_protocol = new Map_protocol();
			map_protocol.setMapid(KIntChange.intChange(memcaHashMap.get("mapid")));
			map_protocol.setX(KIntChange.intChange(memcaHashMap.get("x")));
			map_protocol.setY(KIntChange.intChange(memcaHashMap.get("y")));
			map_protocol.setOperate(MapConfig.getInstance().getEnter());
			map_protocol.setRoleId(id);
			
			channel.write(map_protocol.write());
		}
		
		Role_protocol role_protocol = new Role_protocol();
		role_protocol.setType(SystemDefine.getInstance().getROLE_ID());
		role_protocol.setNames((String) maps.get("name"));
		role_protocol.setUser_id(KIntChange.intChange(maps.get("id")));
		role_protocol.setSex(KIntChange.intChange(maps.get("sex")));
		role_protocol.setLevel(KIntChange.intChange(maps.get("level")));
		
		channel.write(role_protocol.write());
	}
	// end class
}
